package it.univaq.mwt.frontoffice.action;

import java.util.HashSet;
import java.util.Set;

import it.univaq.mwt.business.GiftService;
import it.univaq.mwt.business.ListService;
import it.univaq.mwt.common.business.GiftServiceFactory;
import it.univaq.mwt.common.business.ListServiceFactory;
import it.univaq.mwt.frontoffice.form.ListForm;
import it.univaq.mwt.model.entity.Gift;
import it.univaq.mwt.model.entity.Lists;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class ManageListAction extends MappingDispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		ListServiceFactory listFactory = ListServiceFactory.getInstance();
		ListService facadeList = listFactory.getListService();
		int id_user = Integer.parseInt(session.getAttribute("id_user")
				.toString());

		ListForm listForm = (ListForm) form;

		Lists list = new Lists();
		list.setName(listForm.getName());

		facadeList.addLists(list.getName(), id_user);

		return mapping.findForward("success");
	}

	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		ListServiceFactory listFactory = ListServiceFactory.getInstance();
		ListService facadeList = listFactory.getListService();
		int id_user = Integer.parseInt(session.getAttribute("id_user")
				.toString());

		String id_list = session.getAttribute("id_list").toString();

		ListForm listForm = (ListForm) form;
		Lists list = new Lists();
		list.setName(listForm.getName());

		facadeList.editLists(list.getName(), Integer.parseInt(id_list));
		request.setAttribute("act", "modify");
		return mapping.findForward("success");
	}

	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		ListServiceFactory listFactory = ListServiceFactory.getInstance();
		ListService facadeList = listFactory.getListService();

		GiftServiceFactory giftFactory = GiftServiceFactory.getInstance();
		GiftService facadeGift = giftFactory.getGiftService();

		if (session.getAttribute("id_group") != null) {
			int id_group = Integer.parseInt(session.getAttribute("id_group")
					.toString());
			int id_user = Integer.parseInt(session.getAttribute("id_user")
					.toString());

			Lists list = new Lists();
			list = facadeList.getLists(id_user);

			if (list != null) {
				request.setAttribute("listname", list.getName());
				request.setAttribute("listid", list.getId());
				request.setAttribute("list", list);
				request.setAttribute("act", "modify");
				session.setAttribute("id_list", list.getId());

				Set<Gift> giftlist = new HashSet<Gift>();
				giftlist = facadeGift.getGiftList(list.getId());

				request.setAttribute("giftlist", giftlist);
				// ottengo la lista di gift appartenenti a questa id_list

			}

			// altrimenti creo la pagina per l'inserimeto della lista
			else {
				request.setAttribute("act", "add");
				request.setAttribute("listname", "add list");

			}

			request.setAttribute("actualpage", "whish list");
			return mapping.findForward("edit_list");
		} else
			return mapping.findForward("login");
	}

}
